<?php

namespace App\Support;

use App\Http\Middleware\OpenShop\UserPermissionAuth;
use App\Models\Common\ConfigModel;
use App\Remote\Rms\RmsUserRemote;
use App\Service\OpenShop\Feishu\MessageService;
use Closure;
use GuzzleHttp\Exception\GuzzleException;

class FeishuMsgSupport
{

    protected string         $title;
    protected array          $pushUid;
    protected MessageService $messageService;
    protected array          $content;
    protected string         $module;

    /**
     * @param string $title
     * @param array $pushUid
     */
    private function __construct(string $title, array $pushUid)
    {
        $this->messageService = new MessageService();
        $this->title          = $title;
        $this->pushUid        = $pushUid;
        $this->module         = UserPermissionAuth::MODULE_OPEN_SHOP;
    }

    /**
     * @param string $title
     * @param array $pushUid
     * @return FeishuMsgSupport
     */
    public static function getInstance(string $title, array $pushUid): FeishuMsgSupport
    {
        if (config("app.env") == "qa") {
//            array_push($pushUid, 34);
        }
        return new self($title, $pushUid);
    }

    /**
     * @param mixed ...$content
     * @return $this
     */
    public function setContent(array ...$content): FeishuMsgSupport
    {
        $this->content = $content;
        return $this;
    }

    public function setModule(string $module): FeishuMsgSupport
    {
        $this->module = $module;
        return $this;
    }

    /**
     * 发送消息
     *
     * @param array $queryParams
     * @param string $urlText
     * @param Closure|null $afterClosure
     * @throws GuzzleException
     */
    public function msgSend(array $queryParams, string $urlText = "查看订单", Closure $afterClosure = null)
    {
        $this->messageService->sendMsg(
            $this->content,
            array_filter($this->getPushFsUid()),
            $this->getRedirectUrl($queryParams),
            sprintf("**%s**", $this->title),
            $urlText,
            $this->module
        );
        if ($afterClosure) {
            $afterClosure();
        }
    }


    /**
     * 发送消息
     *
     * @param array $queryParams
     * @param string $urlText
     * @param Closure|null $afterClosure
     * @throws GuzzleException
     */
    public function workOrderMsgSend(array $queryParams, string $urlText = "查看订单", Closure $afterClosure = null)
    {
        $this->messageService->sendMsg(
            $this->content,
            array_filter($this->getPushFsUid()),
            $this->getWorkOrderRedirectUrl($queryParams),
            sprintf("**%s**", $this->title),
            $urlText,
            $this->module
        );
        if ($afterClosure) {
            $afterClosure();
        }
    }

    /**
     * @return array
     * @throws GuzzleException
     */
    private function getPushFsUid(): array
    {
        $rmsUserInfo = RmsUserRemote::getRmsUserByIds($this->pushUid) ?? [];
        return array_column($rmsUserInfo, "feishuUserId");
    }

    /**
     * @param array $queryParams
     * @return string
     */
    private function getRedirectUrl(array $queryParams): string
    {
        $appid = ConfigModel::getFsConfigByPlatformNameOrgId('site', 0)['appId'] ?? '';

        $queryParams = array_merge([
            "appId"    => $appid,
            "mode"     => "window-semi",
            "relaunch" => "true",
            "path"     => ""
        ], $queryParams);

        return "https://applink.feishu.cn/client/mini_program/open?" . http_build_query($queryParams);


    }

    /**
     * @param array $queryParams
     * @return string
     */
    public function getWorkOrderRedirectUrl(array $queryParams): string
    {
        $queryParams = array_merge([
            "appId"    => config("feishu." . UserPermissionAuth::MODULE_WORK_ORDER . ".appID"),
            "mode"     => "window-semi",
            "relaunch" => "true",
            "path"     => ""
        ], $queryParams);

        return "https://applink.feishu.cn/client/web_app/open?" . http_build_query($queryParams);
    }
}
